//
//  MGCScreenCommon.m
//  IOS-Screen
//
//  Created by season02 on 5/2/16.
//  Copyright © 2016 alex.chih. All rights reserved.
//

#import "MGCScreenCommon.h"
#import <UIKit/UIScreen.h>

static float swidth = 0;
//static CGFloat sHeight = 0;

static NSInteger screenSize = 0;

@interface MGCScreenCommon()

//@property (nonatomic, assign) CGFloat swidth;

@end

@implementation MGCScreenCommon

+(NSInteger)deviceSize
{
//    if(screenSize == 0)
//        screenSize = [SDVersion deviceSize];
    return screenSize;
}

+(float)screenWidth
{
    if(swidth == 0)
        swidth = [[UIScreen mainScreen] bounds].size.width;
    return swidth;
}

+(float)screenHeight
{
    if(swidth == 0)
        swidth = [[UIScreen mainScreen] bounds].size.height;
    return swidth;
}

+( UIColor *) getColor:( NSString *)hexColor
{
    unsigned int red, green, blue;
    NSRange range;
    range. length = 2 ;
    range. location = 0 ;
    [[ NSScanner scannerWithString :[hexColor substringWithRange :range]] scanHexInt :&red];
    range. location = 2 ;
    [[ NSScanner scannerWithString :[hexColor substringWithRange :range]] scanHexInt :&green];
    range. location = 4 ;
    [[ NSScanner scannerWithString :[hexColor substringWithRange :range]] scanHexInt :&blue];
    
    return [ UIColor colorWithRed :( float )(red/ 255.0f ) green :( float )(green/ 255.0f ) blue :( float )(blue/ 255.0f ) alpha : 1.0f ];
}
//手机总空间
+ (NSString *) totalDiskSpaceInBytes
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    NSString *diskTotalSize1 = [fattributes objectForKey:NSFileSystemSize];
    return [NSString stringWithFormat:@"总空间%.2fG/" , [diskTotalSize1 floatValue]/1024/1024/1024];
}

//手机剩余空间
+ (NSString *) freeDiskSpaceInBytes{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    //    剩余空间：
    NSString *diskTotalSize2 = [fattributes objectForKey:NSFileSystemFreeSize];
    return [NSString stringWithFormat:@"%@" , [self humanReadableStringFromBytes:[diskTotalSize2 longLongValue]]];
}

+ (float) progress:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    
    
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    //    剩余空间：
    NSString *diskTotalSize2 = [fattributes objectForKey:NSFileSystemFreeSize];
    
//    MGCLog(@"%lld -- %lld",folderSize,[diskTotalSize2 longLongValue]);
    
    float progress = folderSize / (folderSize + [diskTotalSize2 longLongValue]);
    
    return progress;
}

//遍历文件夹获得文件夹大小
+ (NSString *) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return [NSString stringWithFormat:@"0 MB"];
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return  [NSString stringWithFormat:@"%@",[self humanReadableStringFromBytes:folderSize]];
}

//单个文件的大小
+ (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//计算文件大小
+ (NSString *)humanReadableStringFromBytes:(unsigned long long)byteCount
{
    float numberOfBytes = byteCount;
    int multiplyFactor = 0;
    
    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB",@"EB",@"ZB",@"YB",nil];
    
    while (numberOfBytes > 1024) {
        numberOfBytes /= 1024;
        multiplyFactor++;
    }
    if(multiplyFactor > 1){
        return [NSString stringWithFormat:@"%4.2f %@",numberOfBytes, [tokens objectAtIndex:multiplyFactor]];
    } else { return [ NSString stringWithFormat:@"0 MB"]; }
}

@end
